package com.gitee.wsl.model.plugin

import com.gitee.wsl.model.ModelHookHandler
import com.gitee.wsl.model.Model
import com.gitee.wsl.model.ModelHook
import com.gitee.wsl.prop.AttributeKey

open class ModelPluginBuilder<TModel: Model<TModel>, PluginConfig ,Builder:ModelPluginBuilder<TModel,PluginConfig,Builder>>(
    val key: AttributeKey<ModelPluginInstance<TModel,PluginConfig,Builder>>,
    /**
     * A reference to the [TModel] where the plugin is installed.
     **/
     val client: TModel,
    /**
     * A configuration of the current plugin.
     */
     val pluginConfig: PluginConfig
) {

    val hooks: MutableList<ModelHookHandler<TModel,*>> = mutableListOf()
    var onClose: () -> Unit = {}

    /**
     * Specifies the [block] to clean resources allocated with this plugin.
     */
     fun onClose(block: () -> Unit) {
        onClose = block
    }

    /**
     * Specifies a [handler] for a specific [hook].
     * A [hook] can be a specific place in time or event during the request
     * processing like receiving a response, an exception during call processing, etc.
     *
     * @see [createClientPlugin]
     */
     fun <HookHandler> on(
        hook: ModelHook<TModel,HookHandler>,
        handler: HookHandler
    ) {
        hooks.add(ModelHookHandler(hook, handler))
    }
}
